<?php

class CAuthClass extends systemBase {

/*
	public function __construct($lsModuleName) {
		parent::__construct($lsModuleName);
	}
	*/

	public function actionLogin() {
		$lsError = null;
		//
		$this->Smarty->assign("show", "form");
		$this->Smarty->assign("ModuleName", "Přihlášení");
		if(!isLogged()) {
			$this->Smarty->assign("username", getPost("username"));
			// je odeslan formular
			if(IsFormSent("login")) {
				$lsPassword = mandatory(getPost("password"),"Heslo",$lsError);
				$lsUsername = mandatory(getPost("username"),"Uživatel",$lsError);
				$lsPassword = generatePassword($lsPassword);
				if(empty($lsError)) {
					$lsResult = $this->Model->Login(getPost("username"), $lsPassword);
					if($lsResult) {
						// uzivatel je prihlasen
						$lrLogin = array();
						$lrLogin["username"] = $lsResult["USERNAME"];
						$lrLogin["level"] = $lsResult["LEVEL"];
						setSession("Login", $lrLogin);
						redirect(getURL());
					} else {
						// spatne jmeno / heslo
						$lsError = addError($lsError, "Přihlášení", "špatné jméno / heslo");
					}
				}
			}

		}
		$this->setError($lsError);
	}

	public function actionLogout() {
		deleteSession("Login");
		redirect(getURL()."?module=Auth&action=login");
	}

}